﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using MCComDLL;

namespace PcCtrlUI
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{

        static MainWindow singleton;

		public MainWindow()
		{
			this.InitializeComponent();

            singleton = this;

            AppSettingsReader config = new AppSettingsReader();
            temp0.Content = (string)config.GetValue("temp0", typeof(string));
            tempProgress0.setMin((double)config.GetValue("temp0Min", typeof(double)));
            tempProgress0.setMax((double)config.GetValue("temp0Max", typeof(double)));
            tempProgress0.setValue(20);


            temp1.Content = (string)config.GetValue("temp1", typeof(string));
            tempProgress1.setMin((double)config.GetValue("temp1Min", typeof(double)));
            tempProgress1.setMax((double)config.GetValue("temp1Max", typeof(double)));
            tempProgress1.setValue(25);

            temp2.Content = (string)config.GetValue("temp2", typeof(string));
            tempProgress2.setMin((double)config.GetValue("temp2Min", typeof(double)));
            tempProgress2.setMax((double)config.GetValue("temp2Max", typeof(double)));
            tempProgress2.setValue(30);

            temp3.Content = (string)config.GetValue("temp3", typeof(string));
            tempProgress3.setMin((double)config.GetValue("temp3Min", typeof(double)));
            tempProgress3.setMax((double)config.GetValue("temp3Max", typeof(double)));
            tempProgress3.setValue(35);

            temp4.Content = (string)config.GetValue("temp4", typeof(string));
            tempProgress4.setMin((double)config.GetValue("temp4Min", typeof(double)));
            tempProgress4.setMax((double)config.GetValue("temp4Max", typeof(double)));
            tempProgress4.setValue(40);

            temp5.Content = (string)config.GetValue("temp5", typeof(string));
            tempProgress5.setMin((double)config.GetValue("temp5Min", typeof(double)));
            tempProgress5.setMax((double)config.GetValue("temp5Max", typeof(double)));
            tempProgress5.setValue(45);

            temp6.Content = (string)config.GetValue("temp6", typeof(string));
            tempProgress6.setMin((double)config.GetValue("temp6Min", typeof(double)));
            tempProgress6.setMax((double)config.GetValue("temp6Max", typeof(double)));
            tempProgress6.setValue(50);

            temp7.Content = (string)config.GetValue("temp7", typeof(string));
            tempProgress7.setMin((double)config.GetValue("temp7Min", typeof(double)));
            tempProgress7.setMax((double)config.GetValue("temp7Max", typeof(double)));
            tempProgress7.setValue(55);

            temp8.Content = (string)config.GetValue("temp8", typeof(string));
            tempProgress8.setMin((double)config.GetValue("temp8Min", typeof(double)));
            tempProgress8.setMax((double)config.GetValue("temp8Max", typeof(double)));
            tempProgress8.setValue(60);

            temp9.Content = (string)config.GetValue("temp9", typeof(string));
            tempProgress9.setMin((double)config.GetValue("temp9Min", typeof(double)));
            tempProgress9.setMax((double)config.GetValue("temp9Max", typeof(double)));
            tempProgress9.setValue(55);

            temp10.Content = (string)config.GetValue("temp10", typeof(string));
            tempProgress10.setMin((double)config.GetValue("temp10Min", typeof(double)));
            tempProgress10.setMax((double)config.GetValue("temp10Max", typeof(double)));
            tempProgress10.setValue(50);

            temp11.Content = (string)config.GetValue("temp11", typeof(string));
            tempProgress11.setMin((double)config.GetValue("temp11Min", typeof(double)));
            tempProgress11.setMax((double)config.GetValue("temp11Max", typeof(double)));
            tempProgress11.setValue(45);

			
			string name0, name1, name2, name3;
            double max0, max1, max2, max3;
            name0 = (string)config.GetValue("fan0", typeof(string));
			name1 = (string)config.GetValue("fan1", typeof(string));
			name2 = (string)config.GetValue("fan2", typeof(string));
			name3 = (string)config.GetValue("fan3", typeof(string));
			this.fanCtrl0.setNames(name0, name1, name2, name3);
            max0 = (double)config.GetValue("fan0Max", typeof(double));
            max1 = (double)config.GetValue("fan1Max", typeof(double));
            max2 = (double)config.GetValue("fan2Max", typeof(double));
            max3 = (double)config.GetValue("fan3Max", typeof(double));
            this.fanCtrl0.setMax(max0, max1, max2, max3);
            this.fanCtrl0.setValues(100, 200, 300, 400);
            this.fanCtrl0.setFanChan(0);
			
						
            name0 = (string)config.GetValue("fan4", typeof(string));
			name1 = (string)config.GetValue("fan5", typeof(string));
			name2 = (string)config.GetValue("fan6", typeof(string));
			name3 = (string)config.GetValue("fan7", typeof(string));
			this.fanCtrl1.setNames(name0, name1, name2, name3);
            max0 = (double)config.GetValue("fan4Max", typeof(double));
            max1 = (double)config.GetValue("fan5Max", typeof(double));
            max2 = (double)config.GetValue("fan6Max", typeof(double));
            max3 = (double)config.GetValue("fan7Max", typeof(double));
            this.fanCtrl1.setMax(max0, max1, max2, max3);
            this.fanCtrl1.setFanChan(1);
            this.fanCtrl1.setValues(500, 600, 700, 800);
			

            name0 = (string)config.GetValue("fan8", typeof(string));
			name1 = (string)config.GetValue("fan9", typeof(string));
			name2 = (string)config.GetValue("fan10", typeof(string));
			name3 = (string)config.GetValue("fan11", typeof(string));
			this.fanCtrl2.setNames(name0, name1, name2, name3);
            max0 = (double)config.GetValue("fan8Max", typeof(double));
            max1 = (double)config.GetValue("fan9Max", typeof(double));
            max2 = (double)config.GetValue("fan10Max", typeof(double));
            max3 = (double)config.GetValue("fan11Max", typeof(double));
            this.fanCtrl2.setMax(max0, max1, max2, max3);
            this.fanCtrl2.setFanChan(2);
            this.fanCtrl2.setValues(900, 1000, 1100, 1200);
			
			name0 = (string)config.GetValue("flow0", typeof(string));
			name1 = (string)config.GetValue("flow1", typeof(string));
			this.flowCtrl.setNames(name0, name1);
            max0 = (double)config.GetValue("flow0Max", typeof(double));
            max1 = (double)config.GetValue("flow1Max", typeof(double));
            this.flowCtrl.setMax(max0, max1);
            this.flowCtrl.setValue(600, 1200);



            string light0, light1, light2, light3, light4;
            light0 = (string)config.GetValue("light0", typeof(string));
            light1 = (string)config.GetValue("light1", typeof(string));
            light2 = (string)config.GetValue("light2", typeof(string));
            light3 = (string)config.GetValue("light3", typeof(string));
            light4 = (string)config.GetValue("light4", typeof(string));
			
			this.lightCtrl0.setName(light0);
			this.lightCtrl1.setName(light1);
			this.lightCtrl2.setName(light2);
			this.lightCtrl3.setName(light3);
			this.lightCtrl4.setName(light4);

            this.lightCtrl0.Ch = 0;
            this.lightCtrl1.Ch = 1;
            this.lightCtrl2.Ch = 2;
            this.lightCtrl3.Ch = 3;
            this.lightCtrl4.Ch = 4;


            this.lightCtrl0.setValue(20);
            this.lightCtrl1.setValue(40);
            this.lightCtrl2.setValue(60);
            this.lightCtrl3.setValue(80);
            this.lightCtrl4.setValue(100);


            App app = (App)App.Current;
            McCOMChannel mc = app.getMcCOMChannel();
            mc.CallBack = new McCOMChannel.newDataRecv(update);
           
			// Insert code required on object creation below this point.
		}

        private void tempCtrl_Loaded(object sender, RoutedEventArgs e)
        {
            

        }

     


        private void update(McCOMChannel.S_DATA data, McCOMChannel.S_IO_DATA io)
        {
            this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
            {

                fanCtrl0.setValues(data.fanrpm_v0, data.fanrpm_v1, data.fanrpm_v2, data.fanrpm_v3);
               // fanCtrl1.setValues(data.fanrpm_v4, data.fanrpm_v5, data.fanrpm_v6, data.fanrpm_v7);
                //fanCtrl2.setValues(data.fanrpm_v8, data.fanrpm_v9, data.fanrpm_v10, data.fanrpm_v11);
                //flowCtrl.setValue(data.flow_v0, data.flow_v1);

                lightCtrl0.setValue(data.ledpwm_ch0);
                lightCtrl1.setValue(data.ledpwm_ch1);
                lightCtrl2.setValue(data.ledpwm_ch2);

                lightCtrl3.setValue(data.dimm_ch0);
                lightCtrl4.setValue(data.dimm_ch1);

                tempProgress0.setValue(data.tempair_t0);
                tempProgress1.setValue(data.tempair_t1);
                tempProgress2.setValue(data.tempair_t2);
                tempProgress3.setValue(data.tempair_t3);
                tempProgress4.setValue(data.tempair_t4);
                tempProgress5.setValue(data.tempair_t5);
                tempProgress6.setValue(data.tempair_t6);
                tempProgress7.setValue(data.tempair_t7);
                tempProgress8.setValue(data.tempwater_t0);
                tempProgress9.setValue(data.tempwater_t1);
              //  tempProgress10.setValue(data.tempwater_t2);
             //   tempProgress11.setValue(data.tempwater_t3);

                ioCtrl.setValues(io.ENAPC_0, io.ENAPC_1, io.PC_RUNNING, io.S0, io.S1);
                relayCtrl.setValue(0, io.R0);
                relayCtrl.setValue(1, io.R1);
                relayCtrl.setValue(2, io.R2);
                relayCtrl.setValue(3, io.R3);
                relayCtrl.setValue(4, io.R4);
                relayCtrl.setValue(5, io.R5);
                relayCtrl.setValue(6, io.R6);
                relayCtrl.setValue(7, io.R7);
            }));

           

        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	App app = (App)App.Current;
            McCOMChannel mc = app.getMcCOMChannel();
            DateTime date = new DateTime();
            date.AddHours(3);
            mc.setWakeUP(DateTime.Now);  
        }






 
	}
}